package com.ruoyi.mes.service.impl;

import java.math.BigDecimal;
import java.util.List;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.mes.domain.MesDispJobWorkers;
import com.ruoyi.mes.mapper.MesDispJobDoneMapper;
import com.ruoyi.mes.domain.MesDispJobDone;
import com.ruoyi.mes.service.IMesDispJobDoneService;

import javax.annotation.Resource;

/**
 * 完工汇报Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-11-17
 */
@Service
public class MesDispJobDoneServiceImpl implements IMesDispJobDoneService 
{
    @Resource
    private MesDispJobDoneMapper mesDispJobDoneMapper;

    /**
     * 查询完工汇报
     * 
     * @param jobDoneUuid 完工汇报主键
     * @return 完工汇报
     */
    @Override
    public MesDispJobDone selectMesDispJobDoneByJobDoneUuid(String jobDoneUuid)
    {
        return mesDispJobDoneMapper.selectMesDispJobDoneByJobDoneUuid(jobDoneUuid);
    }

    /**
     * 查询完工汇报列表
     * 
     * @param mesDispJobDone 完工汇报
     * @return 完工汇报
     */
    @Override
    public List<MesDispJobDone> selectMesDispJobDoneList(MesDispJobDone mesDispJobDone)
    {
        return mesDispJobDoneMapper.selectMesDispJobDoneList(mesDispJobDone);
    }
    /**
     * 新增时实际汇报数量
     *
     * @param mesDispJobDone 实际数量汇报
     * @return 实际数量
     */
    @Override
    public BigDecimal selectRemainJobDoneQty(MesDispJobDone mesDispJobDone)
    {
        return mesDispJobDoneMapper.selectRemainJobDoneQty(mesDispJobDone);
    }
    /**
     * 修改时实际汇报数量
     *
     * @param mesDispJobDone 实际数量汇报
     * @return 实际数量
     */
    @Override
    public BigDecimal selectRemainEditJobDoneQty(MesDispJobDone mesDispJobDone)
    {
        return mesDispJobDoneMapper.selectRemainEditJobDoneQty(mesDispJobDone);
    }

    /**
     *查询完工汇报表中是否存在当前选择的生产派工
     *
     * @param mesDispJobDone
     * @return fkDispatchUuid
     */
    @Override
    public List<MesDispJobDone> selectFkDispatchUuid(MesDispJobDone mesDispJobDone)
    {
        return mesDispJobDoneMapper.selectFkDispatchUuid(mesDispJobDone);
    }
    /**
     * 新增完工汇报
     * 
     * @param mesDispJobDone 完工汇报
     * @return 结果
     */
    @Transactional
    @Override
    public int insertMesDispJobDone(MesDispJobDone mesDispJobDone)
    {
        int rows = mesDispJobDoneMapper.insertMesDispJobDone(mesDispJobDone);
        insertMesDispJobWorkers(mesDispJobDone);
        return rows;
    }

    /**
     * 修改完工汇报
     * 
     * @param mesDispJobDone 完工汇报
     * @return 结果
     */
    @Transactional
    @Override
    public int updateMesDispJobDone(MesDispJobDone mesDispJobDone)
    {
        mesDispJobDoneMapper.deleteMesDispJobWorkersByFkJobDoneUuid(mesDispJobDone.getJobDoneUuid());
        insertMesDispJobWorkers(mesDispJobDone);
        return mesDispJobDoneMapper.updateMesDispJobDone(mesDispJobDone);
    }

    /**
     * 批量删除完工汇报
     * 
     * @param jobDoneUuids 需要删除的完工汇报主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteMesDispJobDoneByJobDoneUuids(String[] jobDoneUuids)
    {
        mesDispJobDoneMapper.deleteMesDispJobWorkersByFkJobDoneUuids(jobDoneUuids);
        return mesDispJobDoneMapper.deleteMesDispJobDoneByJobDoneUuids(jobDoneUuids);
    }

    /**
     * 删除完工汇报信息
     * 
     * @param jobDoneUuid 完工汇报主键
     * @return 结果
     */
    @Override
    public int deleteMesDispJobDoneByJobDoneUuid(String jobDoneUuid)
    {
        mesDispJobDoneMapper.deleteMesDispJobWorkersByFkJobDoneUuid(jobDoneUuid);
        return mesDispJobDoneMapper.deleteMesDispJobDoneByJobDoneUuid(jobDoneUuid);
    }

    /**
     * 新增完工汇报（工人）信息
     * 
     * @param mesDispJobDone 完工汇报对象
     */
    public void insertMesDispJobWorkers(MesDispJobDone mesDispJobDone)
    {
        List<MesDispJobWorkers> mesDispJobWorkersList = mesDispJobDone.getMesDispJobWorkersList();
        String jobDoneUuid = mesDispJobDone.getJobDoneUuid();
        if (StringUtils.isNotNull(mesDispJobWorkersList))
        {
            List<MesDispJobWorkers> list = new ArrayList<MesDispJobWorkers>();
            for (MesDispJobWorkers mesDispJobWorkers : mesDispJobWorkersList)
            {
                mesDispJobWorkers.setFkJobDoneUuid(jobDoneUuid);
                list.add(mesDispJobWorkers);
            }
            if (list.size() > 0)
            {
                mesDispJobDoneMapper.batchMesDispJobWorkers(list);
            }
        }
    }
}
